package com.huluobo.a2209mdmdemo.myviews

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.navigation.NavHostController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking

/**
 *  Created by LC on 2025/3/7.
 */
class MyView : View {

    //    //一般在代码中创建的时候使用
    constructor(context: Context, navHostController: NavHostController) : super(context) {
        initPaint()
        start()
    }

    //
//    //一般在xml中创建的时候使用 AttributeSet 属性集合,包含了宽高,padding,margin这些属性
//    constructor(context: Context, attributeSet: AttributeSet, navHostController: NavHostController) : super(
//        context,
//        attributeSet
//    ) {
//
//    }

    private lateinit var navHostController: NavHostController

    //画笔
    private lateinit var paint: Paint
    private lateinit var textPaint: Paint

    //绘制的角度
    private var progress = 0f

    private var num = 5

    private fun initPaint() {
        paint = Paint()
        textPaint = Paint()
        //使用android下的color,别使用compose中的color
        paint.color = Color.RED
        textPaint.color = Color.RED
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5f
        textPaint.textSize = 200f
    }

    //用于测量当前view的宽高,包括3种测量模式,AT_MOST,ACTUAL,UN...分别对应warpContent,matchParent,实际的40.dp
    //widthMeasureSpec 包含了mode和size,mode就是测量模式,size就是具体的值
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    //用于当前view中的view的位置的摆放,一般不会出现在view中,有一个例外,是Fragment
    //摆放时,l t r b 分别对应了距离左侧和顶点的位置
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    //用于当前view的绘制,Canvas 画布,当前view的形状如何绘制到画布中,必要条件是需要有个画笔
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val rectF = RectF(100f, 100f, 600f, 600f)
        //绘制矩形
//        canvas.drawRect(rectF, paint)
        //圆范围,起始角度,绘制角度,是否使用圆心,如果不适用,则是首尾相连
        canvas.drawArc(rectF, 270f, progress, false, paint)
        canvas.drawText("$num", 300f, 420f, textPaint)
    }

    fun start() {
        runBlocking {
            val animator = ValueAnimator.ofFloat(360f, 0f)
            animator.addUpdateListener {
                //获取属性动画的变化的值,给绘制角度赋值
                progress = it.animatedValue as Float
                //重绘当前的View
                invalidate()
            }
            animator.interpolator = LinearInterpolator()
            animator.duration = num * 1000L
            animator.start()
        }

        runBlocking {
            val animatorText = ValueAnimator.ofInt(num, 0)
            animatorText.addUpdateListener {
                num = it.animatedValue as Int
                invalidate()
                if (num == 0) {
                    //可以在这里判断当倒计时为0的时候,跳转到首页
                }
            }
            animatorText.interpolator = LinearInterpolator()
            animatorText.duration = num * 1000L
            animatorText.start()
        }
    }
}